package com.example.rabbitmqdemo.cacheTask;


import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public enum FailCallRecordConfig {
    INSTANCE;
    private Map<String, CacheObj<String, List<String>>> cacheMap;
    private ScheduledExecutorService pruneTimer;
    private ThreadPoolExecutor executorListenerPool;

    FailCallRecordConfig() {
        create();
    }

    void create() {
        if (cacheMap == null) {
            System.out.println("--- Config create cacheMap");
            cacheMap = new ConcurrentHashMap<>();
        }
        if (pruneTimer == null) {
            System.out.println("--- Config create pruneTimer");
            pruneTimer = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder()
                    .setNamePrefix("failCallRecord-pushMail-scheduled-pool-")
                    .build());
        }
        if (executorListenerPool == null) {
            System.out.println("--- Config create executorListenerPool");
            int processors = Runtime.getRuntime().availableProcessors();
            executorListenerPool = new ThreadPoolExecutor(processors, 2 * processors,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(),
                    new ThreadFactoryBuilder().setNamePrefix("failCallRecord-pushMail-execute-pool-").build());
        }
    }

    public ThreadPoolExecutor executorListener() {
        return executorListenerPool;
    }

    public ScheduledExecutorService pruneTimer() {
        return pruneTimer;
    }

    public Map<String, CacheObj<String, List<String>>> cacheMap() {
        return cacheMap;
    }

}
